package gold.contest;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC6010 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public long minimumTime(int[] times, int totalTrips) {
            Arrays.sort(times);
            long left = 1, right = 1L * times[0] * totalTrips;// Math.max(((totalTrips * 1L * times[times.length - 1]) / times.length), times[0]);
            while (left < right) {
                long mid = (right - left) / 2 + left;
                if (curCanFinish(times, totalTrips, mid)) {
                    right = mid;
                } else {
                    left = mid + 1;
                }
            }

            return right;
        }

        public boolean curCanFinish(int[] times, int totalTrips, long curTime) {
            int curTotal = 0;
            for (int time : times) {
                long curTri = curTime / time;
                curTotal += curTri;
                if (curTotal >= totalTrips) return true;
            }

            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.minimumTime(InputUtil.toIntegerArray("[1,2,3]"), 5));
        System.out.println(solution.minimumTime(InputUtil.toIntegerArray("[2]"), 1));
        System.out.println(solution.minimumTime(InputUtil.toIntegerArray("[9,7,10,9,10,9,10]"), 1));
        System.out.println(solution.minimumTime(InputUtil.toIntegerArray("[10000]"), 10000000));
    }

    public static void main(String[] args) throws Exception {
        LC6010 an = new LC6010();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
